package com.zhiying.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.plugins.Page;
import com.zhiying.config.StatusEnum;
import com.zhiying.domain.Role;
import com.zhiying.domain.UserVO;
import com.zhiying.mybatis.UserRoleMapper;
import com.zhiying.page.BasePageDTO;
import com.zhiying.page.PageInfo;
import com.zhiying.page.UserPageDTO;
import com.zhiying.service.RoleService;

@Service
@Component
@Transactional
public class RoleServiceImpl implements RoleService {

	@Resource
	private UserRoleMapper userRoleMapper;
	@Override
	public BasePageDTO selectUserRoleInfo(PageInfo pageInfo, String search) {
		// TODO Auto-generated method stub
		Page<BasePageDTO> page=new Page<>(pageInfo.getCurrent(),pageInfo.getLimit());
		List<Role> list=userRoleMapper.selectRoleListInfo(pageInfo, search, page);
		pageInfo.setTotal(selectRoleCount(search));
		return new BasePageDTO<Role>(pageInfo,list);
	}
	@Override
	public int selectRoleCount(String search) {
		// TODO Auto-generated method stub
		return userRoleMapper.selectRoleCount(search);
	}
	
	@Override
	public Role selectRoleInfoById(Long roleid) {
		// TODO Auto-generated method stub
		return userRoleMapper.selectRoleInfoById(roleid);
	}
	@Override
	public int updatestatusByid(Role role) {
		// TODO Auto-generated method stub
		Role roles=selectRoleInfoById(role.getRoleId());
		if (role != null && StatusEnum.NORMAL.getStatus().equals(role.getStatus())) {
			Role updateUser = new Role();
			updateUser.setRoleId(role.getRoleId());
			updateUser.setStatus(0);
			return userRoleMapper.updatestatusByid(updateUser);
		} else if (role != null && StatusEnum.FREEZE.getStatus().equals(role.getStatus())) {
			Role updateUser = new Role();
			updateUser.setRoleId(role.getRoleId());
			updateUser.setStatus(1);
			return userRoleMapper.updatestatusByid(updateUser);
		}
		return 0;
	}
	@Override
	public int selectCmsRoleByRoleId(int roleId) {
		// TODO Auto-generated method stub
		return userRoleMapper.selectCmsRoleByRoleId(roleId);
	}
	@Override
	public int deleteByRoleId(int roleId) {
		// TODO Auto-generated method stub
		return userRoleMapper.deleteByRoleId(roleId);
	}
	@Override
	public UserPageDTO listByRoleId(int roleId, PageInfo pageInfo, String search) {
		// TODO Auto-generated method stub
		Page<UserVO> page=new Page<>(pageInfo.getCurrent(),pageInfo.getLimit());
		List<UserVO> list=userRoleMapper.listByRoleId(roleId, pageInfo, search,page);
		pageInfo.setTotal(selectRoleCountByRoleId(roleId,search));
		return new UserPageDTO(list,pageInfo);
	}
	@Override
	public int selectRoleCountByRoleId(int roleId, String search) {
		// TODO Auto-generated method stub
		return userRoleMapper.selectRoleCountByRoleId(roleId, search);
	}

}
